package com.chen.wxchat.service.impl;

import com.chen.wxchat.dao.AreaDao;
import com.chen.wxchat.entity.Area;
import com.chen.wxchat.entity.Class;
import com.chen.wxchat.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaDao areaDao;

    @Override
    public List<Area> getAreaList() {
        return areaDao.queryArea();
    }

    @Override
    public Area getAreaById(int areaId) {
        return areaDao.queryAreaById(areaId);
    }

    @Override
    @Transactional
    public boolean addArea(Area area) {
        if (area.getAreaName() !=null && !"".equals(area.getAreaName())){
            area.setCreateTime(new Date());
            area.setLastEditTime(new Date());
            try {
                int resultNum = areaDao.insertArea(area);
                if (resultNum > 0){
                    return true;
                }else {
                    throw  new RuntimeException("插入区域信息失败！");
                }
            } catch (Exception e){
                throw  new RuntimeException("插入区域信息失败！" + e.toString());
            }
        }else {
            throw  new RuntimeException("插入区域信息失败！");
        }
    }

    @Override
    public boolean modifyArea(Area area) {
        if (area.getAreaName() !=null && !"".equals(area.getAreaName())){
            area.setCreateTime(new Date());
            area.setLastEditTime(new Date());
            try {
                int resultNum = areaDao.updateArea(area);
                if (resultNum > 0){
                    return true;
                }else {
                    throw  new RuntimeException("插入区域信息失败！");
                }
            } catch (Exception e){
                throw  new RuntimeException("插入区域信息失败！" + e.toString());
            }
        }else {
            throw  new RuntimeException("插入区域信息失败！");
        }
    }

    @Override
    public boolean deleteArea(int areaId) {
        if (areaId > 0){
            try {
                int resultNum = areaDao.deleteArea(areaId);
                if (resultNum > 0){
                    return true;
                }else {
                    throw  new RuntimeException("删除区域信息失败！");
                }
            } catch (Exception e){
                throw  new RuntimeException("删除区域信息失败！" + e.toString());
            }
        }else {
            throw  new RuntimeException("区域信息不完整！");
        }
    }

    @Override
    public Class getClass(int id) {
        return areaDao.getClass(id);
    }
}
